<template>
  <el-dialog title="绑定厂家" v-model="dialogVisible" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form size="" :model="{}" label-width="120px">
      <el-form-item label="用户">
        {{ selectedRows[0]?.nickName }}等{{ selectedRows.length }}个用户
      </el-form-item>
      <el-form-item label="厂家">
        <el-select v-model="carfilm_factory" placeholder="-请选择厂家-" filterable>
          <el-option v-for="(item, index) in factories" :key="index" :label="item.factory_name"
            :value="item.id"></el-option>
          <el-option label="解绑厂家" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="老板账号">
        <el-switch v-model="factory_boss" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import { editUserFactoryApi } from "@/api/carfilm/factory";
import { ElMessage } from "element-plus";
import { inject, ref } from "vue";
const dialogVisible = ref(false);
const factories = inject("factories") as any;
const selectedRows = inject("selectedRows") as any;
const carfilm_factory = ref("");
const factory_boss = ref(0);
const emits = defineEmits(["getTable"]);
const submit = () => {
  let ids = selectedRows.value.map((item: any) => item.user_id);
  editUserFactoryApi({
    user_ids: ids,
    carfilm_factory: carfilm_factory.value,
    factory_boss: factory_boss.value,
  })
    .then((res) => {
      ElMessage({
        message: "操作成功",
        type: "success",
      });
      emits("getTable");
      dialogVisible.value = false;
    })
    .catch((error) => {
      console.log(error);
      ElMessage({
        message: "操作失败",
        type: "error",
      });
    });
};

const open = () => {
  dialogVisible.value = true;
};
defineExpose({
  open,
});
</script>

<style></style>
